from itertools import product


class Prouduct:
    def __init__(self,id,name,price,detail):
        self.name=name
        self.price=price
        self.id=id
        self.detail=detail
class Cart:


    def __init__(self,id):
        self.id=id
        self.cartList=[] # {"product":pr,"count":1}

    def addCart(self,product,n):
        for cart in self.cartList:
            if cart["product"].name==product.name:
                cart["count"] += n
                return {"status":200,"msg":"商品数量添加成功"}

        self.cartList.append({"product":product,"count":n})
        return {"status": 200, "msg": "商品添加成功"}

    def deleteCart(self,product):
        for cart in self.cartList:
            if cart["product"].name==product.name:
                self.cartList.remove(cart)

    def clearCart(self):
        self.cartList.clear()

    def payOrder(self):
        totalPrice = 0
        for cart in self.cartList:
            totalPrice += cart["product"].price * cart["count"]

        return {"status":200,"totalPrice":totalPrice,"msg":"结算成功"}

if __name__ == '__main__':
    product = Prouduct(1,"水杯",15,"喝咖啡很好用")
    product2 = Prouduct(2, "牛奶", 5, "呵呵呵")
    product3 = Prouduct(3, "面包", 9, "很好用")

    cart = Cart(1)
    # 查询购物车
    print(cart.cartList)
    #添加商品到购物车
    cart.addCart(product,1)
    cart.addCart(product2,2)
    cart.addCart(product3,3)

    #查询购物车
    print(cart.cartList)

    #删除购物车
    cart.deleteCart(product)
    print(cart.cartList)

    # 统计价格
    print(cart.payOrder())

    cart.clearCart()
    print(cart.cartList)


